﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace App
{
    public partial class NewEvent : System.Web.UI.Page
    {
        bool prerender = true;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Common.GetLoggedUser(this.Page) == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (prerender)
            {
                SetDropDownListEventForTeam();
                SetSeats();
            }
            AssignTodaysDate();

        }

        private void SetSeats()
        {
            txtboxSeats.Text = MidApp.ManageIsMember.GetNumberOfTeamMembers(MidApp.ManageTeam.GetTeamByTeamName(DropDownListTeamEvent.SelectedItem.ToString()).TeamID).ToString();
        }

        private void SetDropDownListEventForTeam()
        {
            var aux = MidApp.ManageTeam.GetTeamsWhereUserIsLeader(Common.GetLoggedUser(this.Page).UserID);

            foreach (var item in aux)
            {
                DropDownListTeamEvent.Items.Add(item.TeamName);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            AppLib.Event newevent = new AppLib.Event();

            newevent.EventName = txtboxEventName.Text;
            newevent.Address = txtboxAddress.Text;
            newevent.CreatedBy = Common.GetLoggedUser(this).Nick;
            newevent.Price = int.Parse(txtboxPrice.Text);
            newevent.Repeatable = chckRepeatable.Checked;
            newevent.Description = txtboxDescription.Text;
            newevent.DateOfAnnoucing = DateTime.Now;
            newevent.Seats = int.Parse(txtboxSeats.Text);

            //kontrola zda datum neni v minulosti
            try
            {
                DateTime pom = DateTime.Parse(txtboxEventDate.Text);
                pom = pom.AddHours(double.Parse(DropDownList1.SelectedValue));
                pom = pom.AddMinutes(double.Parse(DropDownList2.SelectedValue));

                if (DateTime.Compare(DateTime.Now, pom) > 0)
                {
                    lblInfo.Text = "Nelze vypsat akci v minulosti";
                    Panel2.Visible = true;
                    return;                  
                }
                else
                {
                    newevent.DateOfHolding = pom;            
                }
            }
            catch (Exception)
            {
                lblInfo.Text = "Špatný formát data";
                Panel2.Visible = true;
                return;
            }

            if (chckRepeatable.Checked)
            {
                int span = int.Parse(ListBox1.SelectedItem.Value);
                newevent.SpanBetweenHoldings = int.Parse(ListBox1.SelectedItem.Value);
                DateTime dateOfHolding = newevent.DateOfHolding;
                DateTime dateOfEnding = DateTime.Parse(txtboxFinalDate.Text);
                dateOfEnding = dateOfEnding.AddHours(23);

                Stack<AppLib.Event> stack = new Stack<AppLib.Event>();

                while (dateOfHolding <= dateOfEnding)
                {
                    AppLib.Event novyEvent = new AppLib.Event();

                    novyEvent.Address = newevent.Address;
                    novyEvent.CreatedBy = newevent.CreatedBy;
                    novyEvent.DateOfAnnoucing = newevent.DateOfAnnoucing;
                    novyEvent.DateOfHolding = dateOfHolding;
                    novyEvent.Description = newevent.Description;
                    novyEvent.EventID = newevent.EventID;
                    novyEvent.EventName = newevent.EventName;
                    novyEvent.Participates = newevent.Participates;
                    novyEvent.Price = newevent.Price;
                    novyEvent.Repeatable = newevent.Repeatable;
                    novyEvent.Seats = newevent.Seats;
                    novyEvent.SpanBetweenHoldings = newevent.SpanBetweenHoldings;

                    stack.Push(novyEvent);

                    dateOfHolding = dateOfHolding.AddDays(span);
                }
                MidApp.ManageEvent.CreateNewEvents(stack, MidApp.ManageTeam.GetTeamByTeamName(DropDownListTeamEvent.SelectedItem.ToString()).TeamID, CheckBoxAllTeam.Checked);
            }
            else
            {
                MidApp.ManageEvent.CreateNewEvent(newevent, MidApp.ManageTeam.GetTeamByTeamName(DropDownListTeamEvent.SelectedItem.ToString()).TeamID,CheckBoxAllTeam.Checked);                
           }

            Response.Redirect("EventsList.aspx");
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            prerender = false;
            Panel4.Visible = !Panel4.Visible;
            txtboxFinalDate.Visible = !txtboxFinalDate.Visible;
            Label8.Visible = !Label8.Visible;
            CompareValidator2.Enabled = true;
            RequiredFieldValidator5.Enabled = true;
        }

        public void AssignTodaysDate()
        {
            CalendarExtender1.StartDate =  DateTime.Today;
            txtboxFinalDate_CalendarExtender.StartDate = DateTime.Today;
        }

        protected void CheckBoxAllTeam_CheckedChanged(object sender, EventArgs e)
        {
            prerender = !prerender;
            SetSeats();
            txtboxSeats.Enabled = !txtboxSeats.Enabled;
            
        }

        protected void DropDownListTeamEvent_SelectedIndexChanged(object sender, EventArgs e)
        {
            prerender = false;
            SetSeats();

        }
   
    }
}